const AuthorModel = require('../db/models/author');

class AuthorService {
    //爬取作者信息
    async addAuthorData (data) {
        const aid = data.aid;

        const result = await AuthorModel.findOne({
            where: { aid }
        });

        if (result) {
            return await AuthorModel.update(data, {
                where: { aid }
            })
        } else {
            return await AuthorModel.create(data);
        }
    }

    //查询作者信息
    async getAuthorData () {
        return await AuthorModel.findAll({
            attributes: {
                exclude: ['aid', 'imgUrl', 'desDetail', 'createdAt', 'updatedAt']
              }
        })
    }

    //改变作者上下架状态
    async changeAuthorStatus (id, status) {
        const ret = await AuthorModel.update( { status }, {
            where: { id }
        })

        return ret[0];
    }

    //选择是否为明星作者
    async selectStarAuthor (id, isStar) {
        const ret = await AuthorModel.update( { isStar }, {
            where: { id }
        })

        return ret[0];
    }

    
}

module.exports = new AuthorService();